import os
import sys
import time

import pandas.io.clipboard as cb
import pyautogui
import pygame

import log

# 打开一个app

user_list = []
agreed_date = ""
abspath = ""


# 图像在当前窗口是否存在
def is_exist(img, index=1):
    location = pyautogui.locateCenterOnScreen(resource_path(img), confidence=0.9)
    if location is not None:
        return True
    elif index > 50:
        return False
    else:
        index = index + 1
        time.sleep(0.2)
        is_exist(img, index)


def open_app(index=0):
    location = pyautogui.locateCenterOnScreen(resource_path("logo.png"), confidence=0.9)
    if location is not None:
        log.info("招到可执行程序，双击")
        pyautogui.moveTo(location)
        pyautogui.doubleClick()
        # 点击跳过
        click_tg()
        open_buy_ticket()
    elif index > 50:
        log.info("没有找到可执行程序")
    else:
        index = index + 1
        time.sleep(0.2)
        open_app(index)


def click_tg(index=0):
    location = pyautogui.locateCenterOnScreen(resource_path("tg.png"), confidence=0.9)
    if location is not None:
        pyautogui.moveTo(location)
        pyautogui.doubleClick()
        log.info("点击跳过")
    elif index > 50:
        log.info("没有招到跳过")
    else:
        index = index + 1
        time.sleep(0.2)
        click_tg(index)


# 打开购票约展
def open_buy_ticket(index=0):
    location = pyautogui.locateCenterOnScreen(resource_path("gpyz.png"), confidence=0.9)
    if location is not None:
        time.sleep(2)
        pyautogui.moveTo(location)
        pyautogui.doubleClick()
        log.info("找到购票约展模块，双击")
        # 我知道了
        click_i_see()
    elif index > 50:
        log.info("没有招到购票约展模块")
    else:
        index = index + 1
        time.sleep(0.2)
        open_buy_ticket(index)


# 我知道了
def click_i_see(index=0):
    index = index + 1
    gpyz = pyautogui.locateCenterOnScreen(resource_path("gpyz-max.png"), confidence=0.9)
    location = pyautogui.locateCenterOnScreen(resource_path("wzdl.png"), confidence=0.9)
    if gpyz is not None:
        click_buy_ticket()
    elif location is not None:
        pyautogui.moveTo(location)
        pyautogui.leftClick()
        log.info("点击我知道了")
        time.sleep(0.2)
        click_buy_ticket()
    elif index > 20:
        log.info("没有我知道了")
        click_buy_ticket()
    else:
        time.sleep(1)
        click_i_see(index)


# 点击购票预约
def click_buy_ticket(index=0):
    location = pyautogui.locateCenterOnScreen(resource_path("gpyz-max.png"), confidence=0.9)
    if location is not None:
        pyautogui.moveTo(location)
        pyautogui.leftClick()
        log.info("找到购票约展按钮，点击")
        # click_zdrq()
        click_sq()
    elif index > 50:
        log.info("没有招到购票约展按钮")
    else:
        index = index + 1
        time.sleep(0.2)
        click_buy_ticket(index)


# 点击授权
def click_sq(index=0):
    location = pyautogui.locateCenterOnScreen(resource_path("sq.png"), confidence=0.9)
    if location is not None:
        log.info("弹出了授权页面")
        location = pyautogui.locateCenterOnScreen(resource_path("yx.png"), confidence=0.9)
        if location is not None:
            pyautogui.moveTo(location)
            pyautogui.leftClick()
            log.info("点击允许")
            click_zdrq()
        else:
            log.info("没有招到允许按钮")
    elif index > 5:
        log.info("等待1秒,没有弹出授权页")
        click_zdrq()
    else:
        index = index + 1
        time.sleep(0.2)
        click_sq(index)


# 点击指定日期
def click_zdrq(index=1):
    location = pyautogui.locateCenterOnScreen(resource_path("zdrq.png"), confidence=0.9)
    if location is not None:
        pyautogui.moveTo(location)
        pyautogui.leftClick()
        log.info("找到指定日期，点击")
        time.sleep(0.2)
        select_date(str(int(agreed_date.split("-")[1])), str(int(agreed_date.split("-")[2])))
    elif index > 15:
        log.info("没有招到指定日期")
        click_fh()
    else:
        index = index + 1
        time.sleep(0.2)
        click_zdrq(index)


# 点击可定
def click_kd():
    location = pyautogui.locateCenterOnScreen(resource_path("kd.png"), confidence=0.9)
    if location is not None:
        pyautogui.moveTo(location)
        pyautogui.leftClick()
        log.info("找到可定，点击")
        time.sleep(0.2)
        click_kd2()


# 可定2
def click_kd2(index=0):
    location = pyautogui.locateCenterOnScreen(resource_path("kd2.png"), confidence=0.9)
    if location is not None:
        pyautogui.moveTo(location)
        pyautogui.leftClick()
        log.info("找到二级可定,点击")
        time.sleep(0.2)
        click_jh()
    elif index > 50:
        log.info("没有招到可定")
    else:
        index = index + 1
        time.sleep(0.2)
        click_kd2(index)


# 点击第一个加号
def click_jh(index=0):
    location = pyautogui.locateCenterOnScreen(resource_path("jh.png"), confidence=0.9)
    if location is not None:
        pyautogui.moveTo(location)
        for user in user_list:
            time.sleep(0.1)
            pyautogui.leftClick()

        log.info("招到加号，点击")
        time.sleep(0.2)
        click_ljyd()
    elif index > 50:
        log.info("没有招到加号")
    else:
        index = index + 1
        time.sleep(0.2)
        click_jh(index)


# 点击立即预定
def click_ljyd(index=0):
    location = pyautogui.locateCenterOnScreen(resource_path("ljyd.png"), confidence=0.9)
    if location is not None:
        pyautogui.moveTo(location)
        pyautogui.leftClick()
        log.info("点击立即预定")
        time.sleep(0.2)
        input()
    elif index > 50:
        log.info("没有招到立即预约按钮")
    else:
        index = index + 1
        time.sleep(0.2)
        click_ljyd(index)


# 输入信息
def input():
    h1 = 400
    h3 = -400
    if abspath == 'res/2560_1440_1':
        h1 = 280
        h3 = -350

    log.info("开始填写信息")

    index = 1
    for user in user_list:
        gz = pyautogui.locateCenterOnScreen(resource_path("gz" + str(index) + ".png"), confidence=0.9)
        if gz is not None:
            region = (gz.x - 50, gz.y - 50, 600, h1)
            # jt(region)
            xm = pyautogui.locateCenterOnScreen(resource_path("xm.png"), region=region, confidence=0.9)
            if xm is not None:
                log.info("信息填写开始")
                pyautogui.moveTo(xm.x + 200, xm.y)
                pyautogui.doubleClick()
                pyautogui.leftClick()
                pyautogui.press("delete")
                cb.copy(user[0])
                pyautogui.hotkey('ctrl', 'v')
                pyautogui.moveTo(xm)
                pyautogui.click()
            zjhm = pyautogui.locateCenterOnScreen(resource_path("zjhm.png"), region=region, confidence=0.9)
            if zjhm is not None:
                pyautogui.moveTo(zjhm.x + 200, zjhm.y)
                pyautogui.doubleClick()
                pyautogui.press("delete")
                cb.copy(user[1])
                pyautogui.hotkey('ctrl', 'v')
                pyautogui.moveTo(zjhm)
                pyautogui.click()
            # sjhm = pyautogui.locateCenterOnScreen(resource_path("sjhm.png"), region=region, confidence=0.9)
            # if sjhm is not None:
            #     pyautogui.moveTo(sjhm.x + 200, zjhm.y)
            #     pyautogui.doubleClick()
            #     pyautogui.press("delete")
            #     cb.copy(user[2])
            #     pyautogui.hotkey('ctrl', 'v')
            #     pyautogui.moveTo(sjhm)
            #     pyautogui.click()
            pyautogui.scroll(h3)
            time.sleep(0.2)
        else:
            log.info("未找到页面")

        index = index + 1

    pyautogui.moveRel(0, 100, duration=0.2)
    pyautogui.leftClick()
    log.info("信息填写完毕")
    click_qd()


def click_qd(index=0):
    location = pyautogui.locateCenterOnScreen(resource_path("qd.png"), confidence=0.9)
    if location is not None:
        pyautogui.moveTo(location)
        pyautogui.leftClick()
        log.info("点击确定")
        time.sleep(0.2)
        click_xyb()
    elif index > 50:
        log.info("没有招到确认按钮")
    else:
        index = index + 1
        time.sleep(0.2)
        click_qd(index)


# 点击下一步
def click_xyb(index=0):
    location = pyautogui.locateCenterOnScreen(resource_path("xyb.png"), confidence=0.9)
    if location is not None:
        pyautogui.moveTo(location)
        pyautogui.doubleClick()
        log.info("点击下一步")
        time.sleep(0.5)
        click_tjdd()
    elif index > 50:
        log.info("没有发现下一步按钮")
    else:
        index = index + 1
        time.sleep(0.2)
        click_xyb(index)


def click_tjdd(index=0):
    gpxz = pyautogui.locateCenterOnScreen(resource_path("gpxz.png"), confidence=0.9)
    if gpxz is not None:
        location = pyautogui.locateCenterOnScreen(resource_path("tjdd.png"), confidence=0.9)
        if location is not None:
            pyautogui.moveTo(location)
            pyautogui.leftClick()
            log.info("点击提交订单")
            time.sleep(2)
            click_tjdd2()
        else:
            log.info("没有找到提交按钮")
    elif index > 50:
        log.info("没有招到提交订单按钮")
    else:
        index = index + 1
        time.sleep(0.2)
        click_tjdd(index)


def click_tjdd2(index=0):
    location = pyautogui.locateCenterOnScreen(resource_path("tjdd.png"), confidence=0.9)
    if location is not None:
        pyautogui.moveTo(location)
        pyautogui.leftClick()
        log.info("点击提交订单")
        pdcg()
    elif index > 50:
        log.info("没有招到提交订单按钮")
    else:
        index = index + 1
        time.sleep(0.2)
        click_tjdd2(index)


def pdcg():
    time.sleep(5)
    wc = pyautogui.locateCenterOnScreen(resource_path("wc.png"), confidence=0.9)
    qrdd = pyautogui.locateCenterOnScreen(resource_path("qrdd.png"), confidence=0.9)
    if wc is not None:
        log.info("订单已完成")
        bfyp()
    elif qrdd is not None:
        fj_x = pyautogui.locateCenterOnScreen(resource_path("fhx.png"), confidence=0.9)
        if fj_x is not None:
            pyautogui.moveTo(fj_x)
            pyautogui.leftClick()
            time.sleep(0.5)
            click_tjdd()
        else:
            log.info("没有找到返回")
    else:
        log.info("卡住了吧，你还是切手动吧")


# 可定
def is_kd():
    location = pyautogui.locateCenterOnScreen(resource_path("kd.png"), confidence=0.9)
    if location is not None:
        return True
    else:
        return False


def click_x():
    location = pyautogui.locateCenterOnScreen(resource_path("X.png"), confidence=0.9)
    if location is not None:
        pyautogui.moveTo(location)
        pyautogui.leftClick()
        time.sleep(0.1)
        click_fh()


def click_fh():
    location = pyautogui.locateCenterOnScreen(resource_path("fh.png"), confidence=0.9)
    if location is not None:
        pyautogui.moveTo(location)
        pyautogui.leftClick()
        time.sleep(0.2)
        click_i_see()


# 当前所在模块 1 购票约占模块 2 首页
def is_module():
    tg = pyautogui.locateCenterOnScreen(resource_path("tg.png"), confidence=0.9)
    gpyz_max = pyautogui.locateCenterOnScreen(resource_path("gpyz-max.png"), confidence=0.9)
    gpyz = pyautogui.locateCenterOnScreen(resource_path("gpyz.png"), confidence=0.9)
    if tg is not None:
        pyautogui.moveTo(tg)
        pyautogui.leftClick()
        return is_module()
    elif gpyz_max is not None:
        return 1
    elif gpyz is not None:
        return 2
    else:
        time.sleep(0.2)
        return is_module()


# 生成资源文件目录访问路径
def resource_path(relative_path):
    if getattr(sys, 'frozen', False):
        base_path = sys._MEIPASS
    else:
        base_path = os.path.abspath("")
    return os.path.join(base_path, abspath + "/" + relative_path)


def start_task(users, date, path):
    global user_list
    global agreed_date
    global abspath
    user_list = users
    agreed_date = date
    abspath = path
    open_app()


def jt(region):
    pyautogui.screenshot('res/aaaaa.png', region=region)


def select_date(mon, day, index=0, qxzcgrq=None):
    # 1 鼠标先移动到请选择参观日期下200
    h1 = 230
    h2 = 100
    h3 = -550
    if abspath == 'res/2560_1440_1':
        h1 = 150
        h2 = 80
        h3 = -550

    if qxzcgrq is None:
        qxzcgrq = pyautogui.locateCenterOnScreen(resource_path("qxzcgrq.png"), confidence=0.9)

    if qxzcgrq is not None:
        pyautogui.moveTo(qxzcgrq.x, qxzcgrq.y + 200)
        # jt((qxzcgrq.x - 150, qxzcgrq.y - 50, 300, h1))
        monLocate = pyautogui.locateOnScreen(resource_path(mon + "m.png"), confidence=0.9, region=(
            qxzcgrq.x - 150, qxzcgrq.y - 50, 300, h1))
        if monLocate is not None:
            dayLocate = pyautogui.locateCenterOnScreen(resource_path(day + ".png"), confidence=0.9)
            if dayLocate is not None:
                # jt((dayLocate.x - 30, dayLocate.y - 15, 60, h2))
                kd = pyautogui.locateOnScreen(resource_path('kd.png'), region=(
                    dayLocate.x - 30, dayLocate.y - 15, 60, h2), confidence=0.9)
                if kd is not None:
                    pyautogui.moveTo(dayLocate)
                    pyautogui.leftClick()
                    log.info("找到可定，点击")
                    time.sleep(0.2)
                    click_kd2()
                else:
                    log.info("指定日期没有可定")
                    click_x()
            else:
                log.info("没有招到指定日期")
        else:
            log.info("当前屏幕没有招到指定月份")
            pyautogui.scroll(h3)
            if 2 > index:
                index = index + 1
                select_date(mon, day, index, qxzcgrq)
    else:
        log.info("没有招到请选择参观日期")
        click_fh()


def bfyp():
    pygame.mixer.init()
    pygame.mixer.music.load(resource_path('hbll.mp3'))
    pygame.mixer.music.set_volume(0.5)
    pygame.mixer.music.play()
    time.sleep(2)

# if __name__ == "__main__":
#     abspath = 'res/2560_1440_1'
#     select_date('6', '25')
# user_list = [
#     ['刘志强1', '37292419940528511X', '37292419940528511X'],
#     ['刘志强2', '37292419940528511X', '37292419940528511X'],
#     ['刘志强3', '37292419940528511X', '37292419940528511X'],
#     ['刘志强4', '37292419940528511X', '37292419940528511X'],
#     ['刘志强5', '37292419940528511X', '37292419940528511X']
# ]
# input()
